#!/usr/bin/python
# -*- coding: UTF-8 -*-

import MySQLdb

class MysqlManager:

	db = MySQLdb.connect("localhost", "root", "123456", "wwyc", charset='utf8' )

	def insert(self, sql):
		# 使用cursor()方法获取操作游标 
		cursor = self.db.cursor()

		try:
		   # 执行sql语句
		   cursor.execute(sql)
		   # 提交到数据库执行
		   self.db.commit()
		   return True
		except:
		   # Rollback in case there is any error
		   self.db.rollback()
		   print "Error: insert error"

		   return False

	def update(self, sql):
		# 使用cursor()方法获取操作游标 
		cursor = self.db.cursor()

		try:
		   # 执行sql语句
		   cursor.execute(sql)
		   # 提交到数据库执行
		   self.db.commit()
		   return True
		except:
		   # Rollback in case there is any error
		   self.db.rollback()
		   print "Error: update error"

		   return False

	def select(self, sql):
		try:
			cursor = self.db.cursor()
		    # 执行SQL语句
			cursor.execute(sql)
		    # 获取所有记录列表
			results = cursor.fetchall()
			return results
	    
		except:
		   	print "Error: unable to fecth data"


	def close(self):
		# 关闭数据库连接
		self.db.close()


	

